/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.utility;

/**
 * This class can be used in replacing special characters in XML documents, i.e.
 * & should be replaced by &amp;, < should be replace by &lt; and > should be replaced
 * by &gt;. If these characters are not replaced, they will be send to the server that
 * tries to parse the XML document. However, since the document eventually will 
 * contain these special characters, there will appears errors. 
 */
public class StringReplacer {
	
	public String replace(String text, String searchStr, String replacementStr) {
		StringBuffer sb = new StringBuffer();
        int searchStringPos = text.indexOf(searchStr);
        int startPos = 0;
        int searchStringLength = searchStr.length();
        while (searchStringPos != -1) {
            sb.append(text.substring(startPos, searchStringPos)).append(replacementStr);
            startPos = searchStringPos + searchStringLength;
            searchStringPos = text.indexOf(searchStr, startPos);
        }
        sb.append(text.substring(startPos, text.length()));
        return sb.toString();
    } 
}
